//
//  YKTabBar.m
//  youkan
//
//  Created by mac mini on 16/2/24.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "YKTabBar.h"
/** 自定义带有气泡的TabBar */
#import "YKTabBarButton.h"
@interface YKTabBar()
/** 中间自定义的加号按钮 */
@property(nonatomic,weak)UIButton *plusButton;
/** 存储所有的按钮 */
@property(nonatomic,strong)NSMutableArray *buttons;
/** 记录当前选中的按钮 */
@property(nonatomic,weak)UIButton *selectedButton;

@end

@implementation YKTabBar
-(NSMutableArray *)buttons{
    if (!_buttons) {
        _buttons =[NSMutableArray array];
    }
    return _buttons;
}
-(void)setItems:(NSArray<UITabBarItem*> *)items{
    _items =items;
    for (UITabBarItem *item in _items) {
        YKTabBarButton *btn =[YKTabBarButton buttonWithType:UIButtonTypeCustom];
        btn.item =item;
//        btn.backgroundColor =[UIColor redColor];
        btn.tag = self.buttons.count;
        [btn addTarget: self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        if (btn.tag ==0) {  //!>  默认选中第一个btn
            [self btnClick:btn];
        }
        [self addSubview:btn];
//        YKLog(@"btn==%@",btn);
        [self.buttons addObject:btn];
    }
}
-(void)btnClick:(UIButton *)button{
    _selectedButton.selected =NO;  //!> 将上一个按钮的选中状态修改成未选中
    button.selected = YES;
    _selectedButton = button;
    // 通知tabBarVc切换控制器，
//    -(void)tabBar:(YKTabBar*)tabBar didClickButton:(NSInteger)index;
    if ([_delegate respondsToSelector:@selector(tabBar:didClickButton:)]) {
        [_delegate tabBar:self didClickButton:button.tag];
    }
}
-(UIButton *)plusButton{
    if (!_plusButton) {
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_add"] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(clickPlusButton) forControlEvents:UIControlEventTouchUpInside];
        // 默认按钮的尺寸跟背景图片一样大
        // sizeToFit:默认会根据按钮的背景图片或者image和文字计算出按钮的最合适的尺寸
        //            self.plusButton.bounds =CGRectMake(0, 0, self.plusButton.currentBackgroundImage.size.width, self.plusButton.currentBackgroundImage.size.height);
        [btn sizeToFit];
        _plusButton =btn;

        [self addSubview:_plusButton];
    }
    return _plusButton;
}
-(void)clickPlusButton{
    if ([_delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
        [_delegate tabBarDidClickPlusButton:self];
    }
}
/** 给子控件进行布局 */
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat w = self.bounds.size.width;
    CGFloat h = self.bounds.size.height;
//!>    self.items 是将系统自带的传过来的
    CGFloat btnW = self.bounds.size.width/(self.items.count+1);
    CGFloat btnH = self.bounds.size.height;

    NSInteger i=0;
    for (UIView *tabBarButton in self.buttons) {
        if (i==2) {//!> 跳过中间的加号按钮
            i=3;
        }
        btnX = i*btnW;
        tabBarButton.frame =CGRectMake(btnX, btnY, btnW, btnH);
        i++;
    }
    /*
//    for (UIView *tabBarButton in self.subviews) {
//        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
//            if (i==2) {
//                i++;
//            }
//            btnX = i*btnW;
//            tabBarButton.frame =CGRectMake(btnX, btnY, btnW, btnH);
//            i++;
//        }
//    }*/
    self.plusButton.center =CGPointMake(w*0.5, h*0.5);
}

@end
